<?php
//商品控制器类
class GoodsController extends CommonController{
	private $model;
	/**
	 * [__init 构造函数]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K("Goods");
	}
    //动作方法
    public function index(){
    	// 获取分类栏目数据
    	$cateData = K('Category')->get_data();
    	$arr = array();
    	foreach ($cateData as $k => $v) {
    		if ($v['pid']==0) {
    			$arr[$k]= $v;
    			$arr[$k]["son"]=$this->get_son($cateData,$v['cid']);
    		}
    	}
    	$cateData = $arr;
        $this->assign('cateData',$cateData);
        // 获取产品的数据
        $gid = Q('get.gid',0,'intval');
        $goodsData=$this->model->get_one_data(array('gid'=>$gid));
        if (!$goodsData) {
           $this->success('商品不存在',__ROOT__);
        }
        // 获取商品提问信息
        $askData = K('Ask')->get_data(array('gid'=>$gid));
        // 获取当前商品的分类信息
        $cid =$goodsData['cid'];
        $cateInfo =K('Category')->get_one_data(array('cid'=>$cid));
        // 获得当前商品同分类下的其它商品
        $goodsArr =$this->model->get_data(array('cid'=>$cid),10); 
        // 商品详情数据
        $detailData=k('GoodsDetail')->get_one_data(array('gid'=>$gid));
        $detailData['pic']=explode(',', $detailData['big']);
        // 获取浏览过的商品
        if(isset($_SESSION['browse'])){
            $browse = $_SESSION['browse'];
            $browse = unserialize($browse);
            $browseGid = implode(',', $browse);
            $where = 'gid in ('.$browseGid .')';
            $browseData = $this->model->get_data($where,3);
            $this->assign("browseData",$browseData);
        }else{
            $browse = array(0,0,0);
        }
        array_pop($browse);
        // 把当前浏览的产品ID放入SESSION
        array_unshift($browse, $gid);
        $_SESSION['browse'] = serialize($browse);
        // 计算总库存********************************
        $goodsCount = K('GoodsList')->where(array('gid'=>$gid))->sum('inventory');
        if (!$goodsCount) {
            $goodsCount = 0;
        }
        //商品属性***********************************
        $attr = K('GoodsAttr')->where(array('gid'=>$gid))->group('gtvalue')->order('gaid desc')->all();
        // p($attr);
        $tempOne = array();
        foreach ($attr as $k => $v) {
            $tempOne[$v['taid']][] = $v;
        }
        // p($tempOne);
        $tempTwo = array();
        $typeAttrModel = K('TypeAttr');
        foreach ($tempOne as $k => $v) {
            $tempTwo[] = array(
                'tname' => $typeAttrModel->where(array('taid'=>$k,'class'=>'1'))->getField('taname'),
                'option' => $v
                );
        }
        // p($tempTwo);
        $tempThree = array();
        foreach ($tempTwo as $v) {
           if(!empty($v['tname'])){
               $tempThree[]=$v;
           } 
        }
        // p($tempThree);
        $this->assign('typeAttr',$tempThree);
        $commentData = K('comment')->get_data(array('gid'=>$gid));
        //******************************************
        // 分配所有变量
        $this->assign("goodsArr",$goodsArr);
        $this->assign("askData",$askData);
        $this->assign("cateInfo",$cateInfo);
        $this->assign('goodsData',$goodsData);
        $this->assign('detailData',$detailData);
        $this->assign("goodsCount",$goodsCount);
        $this->assign("commentData",$commentData);
        //显示视图
        $this->dis("shop.html");
    }
    public function check_list(){
        if (!IS_AJAX) $this->error('非法请求');
        $gaid = Q('post.gaid');
        array_multisort($gaid);
        $combine = implode(',', $gaid);
        $goodsListData =K('GoodsList')->get_one_data(array('combine'=>$combine));
        if ($goodsListData) {
            echo json_encode($goodsListData);
            die();
        }else{
            echo 0;
            die();
        }

    }
}
